/*
 * pso.cc
 * The main PSO function calls.
 *  Created on: 13 Feb, 2009
 */

#include "pso.h"
#include "utilities.h"


// Definition of @psoParameters constructor

psoParameters::psoParameters(int dim, int obj){
  particles = 50;
  inertiaWeight = 1.0;
  alpha = 0.98;
  c1 = c2 = 2.05;

  dimensions = dim;
  gBestValue = INF;
  gBestPosition.resize(dim);

  objective = obj;

};

// definition of Particle struct constructor
Particle::Particle(vector<double> initialPosition, int obj){
  // set the dimensions of the particle to be the size of the position Vector
  dimensions = int(initialPosition.size());

  // Set the current position as the given initialPosition
  // The initialPosition is a randomly initialised value generated in the Main function
  currentPosition = initialPosition;

  // The initial velocity of the particle is set as 0
  particleVelocity.resize(dimensions);

  //Naturally the initial position is the best value
  pBest = initialPosition;

  //what's the Objective function based on !
  objective = obj;


}

void Particle::updateParticle(struct psoParameters &globals){

  currentPosition = nextPosition;

  for (int dim = 0; dim < dimensions; ++dim) {
    //do the updation of the velocity here
    double r1 = double(rand())/RAND_MAX;
    double r2 = double(rand())/RAND_MAX;
    double temp = globals.inertiaWeight * particleVelocity[dim];
    temp += globals.c1 * r1 * (pBest[dim]-currentPosition[dim]);
    temp += globals.c2 * r2 * (globals.gBestPosition[dim]-currentPosition[dim]);
    particleVelocity[dim] = temp;

    //velocity updations is over. Do position Updation now
    nextPosition[dim] = currentPosition[dim] + particleVelocity[dim];
  }

  return;
}

double Particle::evaluateParticle(){
  double ans=0.0;
  return ans;
}

bool Particle::updateBest(struct psoParameters &globals){
  return true;
}
